package pe.com.csi.pm.regexiv;

import pe.com.csi.pm.regexiv.util.UConstante;
import pe.com.csi.pm.regexiv.widget.MyEditText;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;

import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Images.Media;

import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

import android.widget.Gallery;
import android.widget.ImageButton;

import android.widget.Toast;

public class TabElementoVial extends Activity {

	private MyEditText txtPI;
	private MyEditText txtPF;

	private ImageButton btnFotos;
	private Cursor mCursor;
	private Gallery mGallery;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.tab_elementovial);

		txtPI = (MyEditText) findViewById(R.id.txtPI);
		txtPF = (MyEditText) findViewById(R.id.txtPF);

		btnFotos = (ImageButton) findViewById(R.id.btnFotos);
		mGallery = (Gallery) findViewById(R.id.tblImagenesE);

		btnFotos.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				i.putExtra(MediaStore.EXTRA_OUTPUT, Media.EXTERNAL_CONTENT_URI.toString());
				startActivityForResult(i, UConstante.CAMERA_ACTIVITY);
			}
		});

	}

	@Override
	protected void onPause() {
		super.onPause();
		// Tramo itemtramo = (Tramo) cmbTramo.getSelectedItem();

		SharedPreferences sp = getSharedPreferences("ELEMENTO_VIAL", MODE_PRIVATE);

		/*
		 * if(itemtramo!=null){ sp.edit().putString("TRAMO",
		 * String.valueOf(itemtramo.getIdTmo())).commit();
		 * sp.edit().putString("IDRUTA",
		 * String.valueOf(itemtramo.getNumeroRta())).commit();
		 * sp.edit().putString("IDCCES",
		 * String.valueOf(itemtramo.getIdCces())).commit();
		 * 
		 * }
		 */
		sp.edit().putString("PROGI", txtPI.getText().toString()).commit();
		sp.edit().putString("PROGF", txtPF.getText().toString()).commit();
		/*
		 * sp.edit().putString("LONGI",
		 * txtLongitudI.getText().toString()).commit();
		 * sp.edit().putString("LONGF",
		 * txtLongitudF.getText().toString()).commit();
		 * sp.edit().putString("LATI",
		 * txtLatitudI.getText().toString()).commit();
		 * sp.edit().putString("LATF",
		 * txtLatitudF.getText().toString()).commit();
		 */

	}

	private void displayGallery() {
		try {
			Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
			String[] projection = { BaseColumns._ID, MediaColumns.TITLE, MediaStore.Images.ImageColumns.LATITUDE, MediaStore.Images.ImageColumns.LONGITUDE, MediaColumns.DISPLAY_NAME, };
			String selection = MediaColumns.TITLE + " = \"" + UConstante.OUR_TITLE_ELEMENTO + "\"";
			mCursor = this.managedQuery(uri, projection, selection, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
				ImageAdapter adapter = new ImageAdapter(mCursor, this);
				mGallery.setAdapter(adapter);
			} else
				showToast(this, "No hay imagenes.");
		} catch (Exception e) {
			showToast(this, e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == UConstante.CAMERA_ACTIVITY) {
			try {
				ContentResolver cr = getContentResolver();
				final String[] p1 = new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN };
				Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC LIMIT 1 ");
				if (c1 != null) {
					if (c1.moveToFirst()) {
						int id = c1.getInt(0);
						ContentValues values = new ContentValues();
						values.put(MediaColumns.TITLE, UConstante.OUR_TITLE_ELEMENTO);
						String where = MediaStore.Images.ImageColumns._ID + " =" + id;
						cr.update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values, where, null);
					}
					c1.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			displayGallery();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		displayGallery();
	}

	private void showToast(Context mContext, String text) {
		Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
	}

}
